﻿using System;
using System.Data;
using System.Text;
using System.Collections.Generic;

using DreamCube.Foundation.Basic.Utility;

namespace DreamCube.Framework.Utilities.Report
{
    /// <summary>
    /// 报表中的列表对象
    /// </summary>
    public class Table : ElementContainer
    {
        #region "字段"

        /// <summary>
        /// {0}:列表的搜索表格
        /// {1}:控制表格的样式
        /// {2}:表格的ID号
        /// {3}:表格内部的Html内容
        /// </summary>
        private static String table = "{0}<br/><table border='0' cellpadding='0' cellspacing='0' class='{1}' TableID='{2}'>{3}</table>";

        #endregion

        #region "属性"

        /// <summary>
        /// 列表的ID值
        /// </summary>
        public String ID
        {
            get;
            set;
        }

        /// <summary>
        /// 数据源的SQL语句
        /// </summary>
        public String DataSourceSQL
        {
            get;
            set;
        }

        /// <summary>
        /// 空数据时显示的文本内容
        /// </summary>
        public String EmptyDataText
        {
            get;
            set;
        }

        /// <summary>
        /// 设置表格的样式
        /// </summary>
        public String TableStyle
        {
            get;
            set;
        }

        #endregion

        #region "公共方法"
        
        /// <summary>
        /// 根据搜索条件生成Html
        /// </summary>
        /// <param name="conditions">
        /// 如果参数为NULL，则视为第一次生成Html，则会把搜索条件区域都带上；
        /// 否则会认为是后续的查询操作，不会再生成搜索条件区域
        /// </param>
        /// <returns></returns>
        public override String ToHtml(List<String> conditions)
        {
            DataTable data = GetDataTable(conditions);
            StringBuilder trSB = new StringBuilder();
            String searchTable = "";
            for (Int32 i = 0; i < elements.Count; i++)
            {
                //行对象
                if (elements[i].ElementType == Enums.ElementType.TableDataTR || elements[i].ElementType == Enums.ElementType.TableTitleTR)
                {
                    TableTR tr = ElementHelper.Create<TableTR>(elements[i]);
                    trSB.Append(tr.ToHtml(data));
                }
                //如果传入条件参数，则不需要再生成查询区域了
                else if (conditions == null && elements[i].ElementType == Enums.ElementType.SearchConditionTable)
                {
                    SearchConditionTable tb = ElementHelper.Create<SearchConditionTable>(elements[i]);
                    searchTable = tb.ToHtml();
                }
            }
            return String.Format(table, searchTable, this.TableStyle, this.ID, trSB.ToString());
        }

        /// <summary>
        /// 传入一个加载数据的委托方法
        /// </summary>
        /// <param name="loadData"></param>
        /// <returns></returns>
        public override String ToHtml()
        {
            return this.ToHtml(null);
        }

        #endregion

        #region "私有方法"

        /// <summary>
        /// 构造SQL语句
        /// </summary>
        /// <param name="conditions">条件</param>
        /// <returns></returns>
        private String MakeSQL(List<String> conditions)
        {
            if (conditions == null || conditions.Count == 0) return this.DataSourceSQL;
            String query = MyEnumerable.JoinEx(conditions, " or ");
            String sql = this.DataSourceSQL;
            return sql + " where " + query;
        }

        /// <summary>
        /// 获取DataTable对象
        /// </summary>
        /// <returns></returns>
        private DataTable GetDataTable(List<String> conditions)
        {
            DataTable data = null;
            if (LoadDataFromSQL != null)
            {
                try
                {
                    String sql = MakeSQL(conditions);
                    data = (DataTable)LoadDataFromSQL.DynamicInvoke(sql);
                }
                catch (Exception ex)
                {
                    MyLog.MakeLog(ex);
                }
            }
            return data;
        }

        #endregion

        #region "静态委托"

        /// <summary>
        /// 如果定义了这个委托，则根据SQL语句获取数据会调用这个方法进行获取数据
        /// </summary>
        public static Func<String, DataTable> LoadDataFromSQL = null;
        
        #endregion
    }
}
